{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\"http://hilpisch.com/tpq_logo.png\" alt=\"The Python Quants\" width=\"35%\" align=\"right\" border=\"0\"><br><br><br>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Listed Volatility and Variance Derivatives\n",
    "\n",
    "**Wiley Finance (2017)**\n",
    "\n",
    "Dr. Yves J. Hilpisch | The Python Quants GmbH\n",
    "\n",
    "http://tpq.io | [@dyjh](http://twitter.com/dyjh) | http://books.tpq.io\n",
    "\n",
    "<img src=\"http://hilpisch.com/../images/lvvd_cover.png\" alt=\"Derivatives Analytics with Python\" width=\"30%\" align=\"left\" border=\"0\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Variance Futures at Eurex"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Introduction"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Based on the previous chapter, this chapter introduces the Eurex variance futures contracts and their major characteristics. It covers mainly the following topics:\n",
    "\n",
    "* introduction and motivation to and for Eurex variance futures\n",
    "* variance futures concepts needed to understand and trade in Eurex variance futures\n",
    "* example calculation for a variance future illustrating the concepts by numerical results\n",
    "* comparison between variance swaps and futures based on the numerical example"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "By introducing variance futures on the EURO STOXX 50 stock index, Eurex standardizes one of the most popular types of volatility/variance derivatives, namely variance swaps. Such a standardization brings a number of benefits to the markets:\n",
    "\n",
    "* **unified terms**: standardization leads to unified, well-documentad terms\n",
    "* **transparency**: centralized trading at Eurex increases transparency for all market participants\n",
    "* **liquidity**: standardization and centralization increases liquidity (market depth) in the variance futures\n",
    "* **fungibility**: variance futures are fully fungible and can be traded in and out at any point over their life time\n",
    "* **increased market integrity**: clearing by Eurex ensures, among others, low counterparty risk and high risk management standards\n",
    "\n",
    "Variance futures replicate the payoff of Over-the-Counter (OTC) variance swaps and are traded based on OTC conventions in vega notional and at volatility strikes. Daily margin payments based on settlement prices add up to the final settlement payoff of the OTC variance swap. Trading start for the variance futures of Eurex has been the 22. September 2014.\n",
    "\n",
    "Net present value (NPV) effects are accounted for by two different pricing components:\n",
    "\n",
    "* discount factor as for the mark-to-market for variance swaps\n",
    "* accumulated return on modified variation margin (ARMVM) for the daily margin payments\n",
    "\n",
    "In order to perfectly mimic the cash flow of a variance swap with the same terms, variance futures trading makes certain conversions necessary. This is mainly due to the variance swaps being based on end-of-day returns of the respective index (which cannot be exactly known during the day). Therefore, there are two conversions:\n",
    "\n",
    "* **intraday**: intraday trades are booked with the desired quantity and at preliminary futures prices\n",
    "* **end-of-day**: at the end of the day, intraday trades are cancelled and re-booked with the same quantity at the futures settlement prices (taking into account the realized variance for the rest of the day, i.e. since the time the trade was initially booked)\n",
    "\n",
    "All trades are booked and conversions are made by the Eurex T7 trading system. While variance futures are quoted in Vega notional and volatility strikes, bookings and clearings are made only in futures and futures prices. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Variance Futures Concepts"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Standardized trading and settlement in variance futures is based on a number of financial concepts. This section introduces all concepts necessary and provides formal definitions."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Realized Variance"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Realized variance $\\sigma^2$ for the Eurex variance futures is defined as\n",
    "\n",
    "$$\\sigma^2 \\equiv 10000 \\cdot \\frac{252}{N} \\cdot \\sum^{N}_{n=1}R_n^2$$\n",
    "\n",
    "where, for a time series $S_n, n=0, 1, ..., N$, of daily EURO STOXX 50 closing values, the log returns are given by\n",
    "\n",
    "$$R_n \\equiv \\log \\frac{S_{n}}{S_{n-1}}$$\n",
    "\n",
    "Here, it is assumed that there are 252 trading days per year and that the average daily return is zero. Note the scaling factor of 10,000 to get to the market convention of quoting variance in percent and not in decimal values."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Net Present Value Concepts"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "There are two concepts that take into account that the daily margin cash flows of trading futures leads, time-wise, to a different cash flow profile than the single payoff of a variance future at maturity. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Discount Factor"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The first is the discount factor $DF_t$ which is defined for a maturity date $T$ and a current date $t$ by\n",
    "\n",
    "$$DF_t = e^{-\\frac{r (T-t)}{365}}$$\n",
    "\n",
    "Here, $r$ is the relevant interest rate to be applied to the remaining time-to-maturity $(T - t)$, calculated in days. Eurex uses Euribor rates to calculate the discount factor."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let us have a look at the development of the Euribor rates in 2014 from January to the beginning of September. Euribor rates are available on the web site http://www.euribor-rates.eu or a bit more convenient as Excel spreadsheet files for download on the web site http://www.emmi-benchmarks.eu/euribor-org/euribor-rates.html. From this last web site, we have downloaded Euribor rates data for 2014 which we use in the following. For details on the Python usage that follows, see chapter _Python Introduction_ or refer to Hilpisch (2018): _Python for Finance_. pandas is again the library we are working with."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "With pandas, you can read data from a spreadsheet file as follows."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "## read data from CSV file\n",
    "eb = pd.read_csv('./data/hist_EURIBOR_2015.csv',  # filename\n",
    "                   index_col=0,  # index column\n",
    "                   parse_dates=True,  # parsing date information\n",
    "                   dayfirst=True)  # European date convention\n",
    "eb.info()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The following figure shows the evolution of the eight different rates over time (values in percent). "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "from pylab import mpl, plt\n",
    "plt.style.use('seaborn')\n",
    "mpl.rcParams['font.family'] = 'serif'\n",
    "eb.plot(figsize=(10, 6));"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<p style=\"font-family: monospace;\">Historical Euribor rates."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Short term interest rates are pretty low, leading to a rather small impact of the discount factor in such an environment. Let us calculate the discount factor for the longest maturity with the last available rate for the 12 months horizon."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "eb.tail()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For a 12 months period the discount effect is less than half a percent."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "import math\n",
    "math.exp(-0.00168 * 365. / 365)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### ARMVM"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The accumulated return on modified variation margin (ARMVM) takes into account that variance futures trading leads to daily margin payments that have an impact on the present value of the variance future. For given dates $t$ and $t-1$, ARMVM is given by \n",
    "\n",
    "$$ARMVM_t = ARMVM_{t-1}\\cdot e^{\\frac{r\\Delta t}{365}} + \\left(F_{t-1}^S - C \\right)\\left(e^{\\frac{r\\Delta t}{365}}-1\\right)$$\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "Here, $r$ is the relevant interest rate to be used, $\\Delta t$ is the difference between dates $t$ and $t-1$ in days, $F_{t-1}^S$ is the variance futures settlement price at $t-1$ and $C$ is a constant fixed at a level of 3,000. If $t=1$, i.e. on the first settlement day (say Monday), $ARMVM_{1} \\equiv 0$ and $F_{1}^S \\equiv C$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The relevant rate for the ARMVM calculation is the Eonia rate. On the web site http://www.emmi-benchmarks.eu/euribor-eonia-org/about-eonia.html you find the following explanation:\n",
    "\n",
    "> \"Eonia® (Euro OverNight Index Average) is the effective overnight reference rate for the euro. It is computed as a weighted average of all overnight unsecured lending transactions in the interbank market, undertaken in the European Union and European Free Trade Association (EFTA) countries.\"\n",
    "\n",
    "This web site also provides historical Eonia data for download. Let us have a look at the historical development of the Eonia rates for a couple of weeks in 2015."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "## read data from Excel spreadsheet file\n",
    "eo = pd.read_csv('./data/hist_EONIA_2015.csv',  # filename\n",
    "                   index_col=0,  # index column\n",
    "                   parse_dates=True,  # parsing date information\n",
    "                   dayfirst=True)  # European date convention\n",
    "eo.info()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Again, we can easily visualize the data set (see the following figure)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "eo.plot(figsize=(10, 5));"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<p style=\"font-family: monospace;\">Historical Eonia rates."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The last available values in the data set for Eonia are:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "eo.tail()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let us work with a value (in decimals) of $r_{eo} = -0.001$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "r_eo = -0.001"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now consider the second settlement day $t=2$ (say Tuesday), assume that a rate of $r_{eo}$ applies and remember that $F_{1}^S=3000$. We then have\n",
    "\n",
    "$$\n",
    "ARMVM_2 = 0 \\cdot e^{\\frac{r_{eo} \\cdot 1}{365}} + \\left(3000 - 3000 \\right)\\left(e^{\\frac{r_{eo} \\cdot 1}{365}}-1\\right) = 0\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Consider now the third settlement day $t=3$ (say Wednesday). Assume, that the futures settlement price has risen the day before to $F_2^S = 3100$. Now\n",
    "\n",
    "$$\n",
    "ARMVM_3 = 0 \\cdot e^{\\frac{r_{eo} \\cdot 1}{365}} + \\left(3100 - 3000 \\right)\\left(e^{\\frac{r_{eo} \\cdot 1}{365}}-1\\right)\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The effect is rather small."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "ARMVM3 = 0 * math.exp(r_eo / 365) + (3100 - 3000) * (math.exp(r_eo / 365) - 1)\n",
    "ARMVM3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "One day later, on the fourth settlement day $t=4$ and assuming that the variance futures settlement price has fallen the day before to $F_3 = 3050$ we get\n",
    "\n",
    "$$\n",
    "ARMVM_4 = ARMVM_3 \\cdot e^{\\frac{r_{eo} \\cdot 1}{365}} + \\left(3050 - 3000 \\right)\\left(e^{\\frac{r_{eo} \\cdot 1}{365}}-1\\right)\n",
    "$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "ARMVM4 = (ARMVM3 * math.exp(r_eo / 365)\n",
    "        + (3050 - 3000) * (math.exp(r_eo / 365) - 1))\n",
    "ARMVM4"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Traded Variance Strike"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The traded variance strike $\\sigma_t^2$ at date $t$ is the time weighted average of the realized variance $\\sigma_{0,t}^2$ weighted by the number of elapsed days $t$ and the traded implied variance $\\sigma_{i}^2(t,T)$ weighted by the remaining life time of the variance future in days:\n",
    "\n",
    "$$\n",
    "\\sigma_t^2 = \\frac{t \\cdot \\sigma_{0,t}^2 + (T - t) \\cdot \\sigma_{i}^2(t,T)}{T}\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Traded Futures Price"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The traded futures price $F_t$ given the other quantities from before is then defined as\n",
    "\n",
    "$$\n",
    "F_t = DF_t \\cdot \\left(\\sigma_t^2 -\\sigma_{i}^2(t,T) \\right) - ARMVM_t + 3000\n",
    "$$\n",
    "\n",
    "The scaling constant $C=3000$ is chosen to ensure that the futures price cannot become negative."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Number of Futures"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Variance futures are traded in Vega notional and volatility strikes. However, settlement takes place in futures (or variance notional) and variance strikes. The number of futures is calculated according to the formula\n",
    "\n",
    "$$\n",
    "futures = \\frac{VegaNotional}{2 \\cdot \\sigma_{i}(t,T)} \\cdot \\frac{T}{T-t}\n",
    "$$\n",
    "\n",
    "with $\\sigma_{i}(t,T)$ as the traded implied volatility strike."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Par Variance Strike"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "At daily settlement, i.e. when the log return for the EURO STOXX 50 for that day is known, the par variance strike $\\sigma_p^2$ is calculated using the then current, settled implied volatility $\\sigma_i$. \n",
    "\n",
    "$$\n",
    "\\sigma_p^2 = \\frac{t \\cdot \\sigma_{0,t}^2 + (T-t) \\cdot \\sigma_i^2}{T}\n",
    "$$\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Futures Settlement Price"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Finally, the daily futures settlement price $F_t^S$ is calculated according to the following formula where all components are as defined as before.\n",
    "\n",
    "$$\n",
    "F_t^S = DF_t \\cdot \\left(\\sigma_p^2 - \\sigma_K^2\\right) - ARMVM_t + 3000\n",
    "$$\n",
    "\n",
    "The futures settlement price is the mark-to-market value of the corresponding variance swap minus the ARMVM plus the scaling factor of 3,000."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example Calculation for a Variance Future"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this section, we conduct an example calculation for the Eurex variance futures contract given historical data as used in section _Variance Swaps_."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "h5 = pd.HDFStore('./data/SX5E_V6I1.h5', 'r')\n",
    "data = h5['SX5E_V6I1']\n",
    "h5.close()\n",
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We add Euribor and Eonia data to the pandas ``DataFrame`` object. For simplicity, we use the 2 week Euribor values throughout. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "data = data.join(eb['2w'], how='left')\n",
    "data = data.join(eo, how='left')\n",
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let us add the log returns to the data set as well as the realized variance."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "data['R_t'] = np.log(data['SX5E'] / data['SX5E'].shift(1))\n",
    "data['sigma**2'] = 10000 * 252 * (np.cumsum(data['R_t'] ** 2)\n",
    "                                  / np.arange(len(data)))\n",
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Assume that the variance future comes to life on 01. June 2015 and that it matures on 19. June 2015. This is a maturity of $T=15$ trading days. Let us generate in addition an array with all (elapsed) trading days over time."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "T = 15.\n",
    "data['t'] = np.arange(1, 16)\n",
    "data['t']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Assuming a constant Euribor rate of -0.1%, we can add a new column to the ``DataFrame`` object with the discount factors in vectorized fashion (see chapter _Trading and Settlement_ for further details on the discount factor calculation)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "r_eb = -0.001\n",
    "data['DF_t'] = np.exp(-r_eb * (T - data['t']) / 365.) \n",
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The standard volatility strike is $\\sigma_K = 25.871$ and the standard variance strike is $\\sigma_K^2=25.871^2=669.31$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "sigma_K = data['V6I1'][0]\n",
    "sigma_K"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Assume a vega notional of 100,000. This translates into a variance notional of:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "Notional = 100000 / (2 * sigma_K)\n",
    "Notional"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The settlement price on the first trading day is standardized to 3,000. We generate a new column in the pandas ``DataFrame`` object and initialize the first value (and all the others) accordingly."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "data['F_tS'] = 3000\n",
    "\n",
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The ARMVM on the first settlement day is zero. We again generate a new column and initialize all values with zero."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "data['ARMVM_t'] = 0.0"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The futures price on the second settlement day is given as:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pd.options.mode.chained_assignment = None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "data['F_tS'][1] = data['DF_t'][1] * ((data['t'][1] * data['sigma**2'][1]\n",
    "                    + (T - data['t'][1]) * data['V6I1'][1] ** 2) / T\n",
    "                    - sigma_K ** 2) + 3000\n",
    "data['F_tS'][1]                       "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Analogously, we can calculate the settlement prices for all other settlement days. Note that we take the index level of the EURO STOXX 50 at close and not the average of its level between 11:50 and 12:00 CET at the final settlement day (see chapter _Trading and Settlement_)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "for t in data.index[1:]:\n",
    "    data['ARMVM_t'][t] = (data['ARMVM_t'].shift(1)[t]\n",
    "                          * math.exp(data['EONIA'].shift(1)[t] / 252)\n",
    "                       + (data['F_tS'].shift(1)[t] - 3000)\n",
    "                         * (math.exp(data['EONIA'].shift(1)[t] / 252) - 1))\n",
    "    data['F_tS'][t] = data['DF_t'][t] * ((data['t'][t] * data['sigma**2'][t]\n",
    "                          + (T - data['t'][t]) * data['V6I1'][t] ** 2) / T\n",
    "                          - sigma_K ** 2) - data['ARMVM_t'][t] + 3000"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We end up with a complete data set, and in particular the simulated variance futures settlement values. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The following gifure shows the results graphically."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "data[['SX5E', 'sigma**2', 'F_tS']].plot(subplots=True,\n",
    "                                       #color='blue',\n",
    "                                       figsize=(10, 9));"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<p style=\"font-family: monospace;\">Calculated variance futures settlement values for the EURO STOXX 50."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We save the generated data set for re-use in the next section."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "h5 = pd.HDFStore('./data/var_data.h5', 'a')\n",
    "h5['var_future'] = data\n",
    "h5.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Comparison of Variance Swap and Future"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Eurex variance futures are a means to replicate the payoff of OTC-traded variance swaps with a listed, standardized product. Let us compare the mark-to-market values of the variance swap from section _Variandce Swaps_ with the settlement values calculated in the previous section."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To this end, we import the data for the variance swap first."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "h5 = pd.HDFStore('./data/var_data.h5', 'r')\n",
    "var_swap = h5['var_swap']\n",
    "h5.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Next, we combine and plot the two time series for the variance swap and the variance future, respectively, against each other. Note that we subtract the constant $C$ for the comparison."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "comp = pd.DataFrame({'F_tS': data['F_tS'] - 3000,\n",
    "                     'V_t': var_swap['V_t']}, index=data.index)\n",
    "comp"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The following figure compares the two time series."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "comp.plot(style=['b', 'ro'], figsize=(9, 5));"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<p style=\"font-family: monospace;\">Variance swap and futures prices for EURO STOXX 50 compared."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Finally, the following figure presents the absolute differences which are quite small."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "(comp['F_tS'] - comp['V_t']).plot(style='r^', figsize=(9, 5));"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<p style=\"font-family: monospace;\">Differences between variance swap and futures prices."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Conclusions"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This chapter deals with the basic notions and concepts that need to be understood to trade Eurex variance futures based on the EURO STOXX 50 stock index. It introduces all concepts in formal fashion and illustrates their calculation based on a concrete numerical example which is based on the same data as the variance swap example calculation in the previous chapter. This makes it possible to numerically show that indeed the construction of the Eurex variance futures contract replicates the payoff of typical OTC variance contracts while adding intraday liquidity and tradeability. The subsequent chapter covers special aspects that relate to the intraday trading and end-of-day settlement of these derivatives products."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\"http://hilpisch.com/tpq_logo.png\" alt=\"The Python Quants\" width=\"35%\" align=\"right\" border=\"0\"><br>\n",
    "\n",
    "<a href=\"http://tpq.io\" target=\"_blank\">http://tpq.io</a> | <a href=\"http://twitter.com/dyjh\" target=\"_blank\">@dyjh</a> | <a href=\"mailto:team@tpq.io\">team@tpq.io</a>"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
